#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="mtsleep5"
__date__="2016/9/18"
__time__="17:15"
"""

# 三：从Thread派生出一个子类，创建一个这个子类的实例,重写run方法
import threading
from time import sleep,ctime

loops = (4,2)
class MyThread(threading.Thread):
    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self) # 调用父类的构造函数
        self.name = name
        self.func = func
        self.args = args
    def run(self):
        apply(self.func,self.args)
def loop(nloop,nsec):
    print 'start loop',nloop,'at:',ctime()
    sleep(nsec)
    print 'loop',nloop,'done at:',ctime()

def main():
    print 'starting at:',ctime()

    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = MyThread(loop,(i,loops[i]),loop.__name__)   # func = loop ;args = (i,loops[i])  # 实例化
        threads.append(t)
    for i in nloops:
        threads[i].start()  # 执行start()方法时，将运行run()方法。

    for i in nloops:
        threads[i].join()

    print 'all DONE at:',ctime()

if __name__ == '__main__':
    main()